﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("Brand")]
    public partial class Brand : PageBase
    {
        private byte ModelId;
        private readonly BrandManage _myBLL = new BrandManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModelId = byte.Parse(Request.QueryString["ModelId"]);
            if (!IsPostBack)
            {
                if (Request["Action"] == null && Request["ID"] == null)
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelId={1}'>品牌管理</a>",
                        ModelManage.GetModelName(this.ModelId), this.ModelId);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    BindList(1);
                }
                else if (Request["ID"] != null && Request["Action"] == "Eidt")
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelId={1}'>品牌管理</a> → 修改",
                        ModelManage.GetModelName(this.ModelId), this.ModelId);
                    //“修改”界面
                    this.btnOK.Text = "修改";
                    Edit_Load();

                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
                else if (Request["ID"] == null && Request["Action"] == "Add")
                {
                    this.lblPath.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelId={1}'>品牌管理</a> → 添加",
                        ModelManage.GetModelName(this.ModelId), this.ModelId);
                    //“添加”界面
                    this.btnOK.Text = "添加";
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
            }
        }

        #region 添加或修改

        private void Add()
        {
            var model = new WinnerCMS.Model.Brand();

            model.Name = this.txtName.Text.Trim();
            model.SrcList = this.txtSrcList.Text.Trim();
            model.SrcDetail = this.txtSrcDetail.Text.Trim();
            model.Website = this.txtWebsite.Text.Trim();

            model.Content = this.txtContent.Text.Trim();
            model.IsShow = this.cbkIsShow.Checked ? 1 : 0;
            model.Title = this.txtTitle.Text.Trim();
            model.Keywords = this.txtKeywords.Text.Trim();
            model.Description = this.txtDescription.Text.Trim();
            model.Pinyin = this.txtPinyin.Text.Trim();
            model.Company = this.txtCompany.Text.Trim();
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else if (Request["ID"] != null && Request["Action"] == "Edit")
            {
                model.Id = int.Parse(Request["ID"]);
            }

            string strErr = null;

            new BrandManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
            }
        }

        #endregion

        #region 加载

        private void Edit_Load()
        {
            var model = new BrandManage().GetInfo(int.Parse(Request["ID"]));
            if (model != null)
            {
                this.txtName.Text = model.Name;
                this.txtSrcList.Text = model.SrcList;
                this.txtSrcDetail.Text = model.SrcDetail;
                this.txtWebsite.Text = model.Website;

                this.txtContent.Text = model.Content;
                this.cbkIsShow.Checked = model.IsShow == 1 ? true : false;
                this.txtTitle.Text = model.Title;
                this.txtKeywords.Text = model.Keywords;
                this.txtDescription.Text = model.Description;
                this.txtPinyin.Text = model.Pinyin;
                this.txtCompany.Text = model.Company;
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }

        #endregion

        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.GV_List.DataSource = this._myBLL.BindList(pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            if (this.GV_List.Rows.Count <= 0)
            {
                this.btnDelSeletedID.Enabled = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var lb = e.Row.FindControl("lblIsShow") as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"显示\" src=\"../Images/success-true.gif\"> ";
                }
                else if (lb.Text == "0")
                {
                    lb.Text = "<IMG alt=\"不显示\" src=\"../Images/failed-false.gif\"> ";
                }

                var myTableCell = e.Row.Cells[this.GV_List.Columns.Count - 1];
                if (myTableCell.Controls.Count > 0)
                {
                    var btnDel = (LinkButton) myTableCell.Controls[0];

                    btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this._myBLL.Delete(this.GV_List.DataKeys[e.RowIndex].Value.ToString(), this.UpdatePanel1);
            BindList(1);
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "IsShow")
            {
                var ID = (int) e.CommandArgument;

                var _myBLL = new BrandManage();

                var model = _myBLL.GetInfo(ID);
                model.IsShow = (1 - model.IsShow);
                string Err = null;
                _myBLL.Add(model, ref Err);

                BindList(this.PageNavigation1.PageNo);
            }
        }
    }
}